package com.example.javacv;

import org.bytedeco.javacpp.Loader;
import org.bytedeco.javacv.FFmpegFrameGrabber;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CMD {
    public static void main(String[] args) throws IOException, InterruptedException {
        String ffmpeg = Loader.load(org.bytedeco.ffmpeg.ffmpeg.class);
        String path = "D:\\3.mov";
        FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(path);
        grabber.start();
        boolean merge = false;
        int videoTime = (int) grabber.getLengthInTime() / 1000000 / 60;
        int periodTime = 1;

        // 计算视频分割的个数
        int count;// 分割为几段
        float remain = 0; // 不足一次剪辑的剩余时间
        if (merge) {
            count = (int) (videoTime / periodTime);
            remain = videoTime % periodTime; // 不足一次剪辑的剩余时间
        } else {
            count = (int) (videoTime / periodTime) + 1;
        }
        System.out.println("将视频分割为" + count + "段，每段约" + periodTime + "分钟");

        String indexName; // 第 i 个视频，打印日志用
        /*final String FFMPEG = ffmpegPath;*/
        String startTime = ""; // 每段视频的开始时间
        String periodVideoName = ""; // 每段视频的名字，名字规则：视频i_时间段xx_yy
        float duration = 0; // 每次分割的时长
        String command;// 执行的命令
        // 得到视频后缀 如.mp4
        String videoSuffix = ".mov";//得到点后面的后缀，包括点
        Runtime runtime = Runtime.getRuntime(); // 执行命令者

        List<Map<String, Object>> list = new ArrayList<>();
        // 将视频分割为count段
        for (int i = 0; i < count; i++) {
            Map<String, Object> map = new HashMap<>();
            indexName = "第" + (i + 1) + "个视频";

            // 决定是否将整个视频结尾部分不足一次的时间，合并到最后一次分割的视频中
            if (merge) {
                if (i == count - 1) {
                    duration = periodTime * 60 + remain * 60;// 将整个视频不足一次剪辑的时间，拼接在最后一次剪裁中
                    if (periodTime * i / 60 >= 1) {
                        startTime = "0" + periodTime * i / 60 + ":00:00";
                    } else {
                        startTime = periodTime * i + ":00";
                    }
                    periodVideoName = "video" + (i + 1) + "_" + periodTime * i + "_end" + videoSuffix;
                } else {
                    duration = periodTime * 60;
                    if (periodTime * i / 60 >= 1) {
                        startTime = "0" + periodTime * i / 60 + ":00:00";
                    } else {
                        startTime = periodTime * i + ":00";
                    }
                    periodVideoName = "video" + (i + 1) + "_" + periodTime * i + "_" + periodTime * (i + 1) + videoSuffix;
                }
            } else {
                duration = periodTime * 60;
                if (periodTime * i / 60 >= 1) {
                    startTime = "0" + periodTime * i / 60 + ":00:00";
                } else {
                    startTime = periodTime * i + ":00";
                }
                periodVideoName = "video_" + (i + 1) + videoSuffix;
//                periodVideoName = "video" + (i+1) + "_" + periodTime * i + "_" + periodTime * (i + 1) + videoSuffix;
            }
            ProcessBuilder pb = new ProcessBuilder(ffmpeg, "-ss", startTime,
                    "-accurate_seek", "-i", path, "-b:v", "5000k", "-c", "copy", "-t", String.valueOf(duration), "D:\\" + periodVideoName);
            pb.inheritIO().start().waitFor();
        }

    }
}
